package org.myproject.persistence.event;

import java.io.Serializable;

import org.hibernate.persister.entity.EntityPersister;
import org.springframework.context.ApplicationEvent;

/**
 * Event that represents a change in the lifecycle of a persistent entity
 * 
 * @author ivaynberg
 */
public abstract class PersistenceEvent extends ApplicationEvent
{
    private static final long serialVersionUID = 1L;

    /** the entity * */
    private final Object entity;

    /** the entitie's id */
    private final Serializable id;

    /** hibernate entity persister */
    private final EntityPersister persister;

    /**
     * Construct.
     * 
     * @param source
     *                source of the event
     * @param entity
     *                entity
     * @param id
     *                entity id
     * @param type
     *                event type
     */
    public PersistenceEvent(Object source, Object entity, Serializable id, EntityPersister persister)
    {
        super(source);
        this.entity = entity;
        this.id = id;
        this.persister = persister;
    }

    /**
     * Gets entity.
     * 
     * @return entity
     */
    public Object getEntity()
    {
        return entity;
    }

    /**
     * Gets id.
     * 
     * @return id
     */
    public Serializable getId()
    {
        return id;
    }

    public EntityPersister getPersister()
    {
        return persister;
    }

    /**
     * Returns list of property names for the entity
     * 
     * @return
     */
    public String[] getPropertyNames()
    {
        return persister.getPropertyNames();
    }

    @Override
    public String toString()
    {
        return "[" + getClass().getName() + " id:" + id + " class:" + entity.getClass().getName()
                + "]";
    }

}
